/**
 * @description: 校验参数
 * @param {String} type
 * @param {Array} params
 * @return {Boolean} true/false
 * @Author: yx
 */
function validParams(type, params) {
  if (!(params instanceof Array)) {
    throw new Error("请传入数组");
  }
  let valid = true;
  switch (type) {
    case "point":
      let lon, lat;
      params.forEach((point) => {
        lon = point[0];
        lat = point[1];
        if (
          typeof lon !== "number" ||
          typeof lat !== "number" ||
          isNaN(lon) ||
          isNaN(lat) ||
          lon < -180 ||
          lon > 180 ||
          lat < -90 ||
          lat > 90
        ) {
          valid = false;
        }
      });
      break;

    case "line":
      params.forEach((line) => {
        if (!(line instanceof Array)) {
          throw new Error("请检查传入线的格式是否正确");
        }
        line.forEach((point) => {
          let lon, lat;
          lon = point[0];
          lat = point[1];
          if (
            typeof lon !== "number" ||
            typeof lat !== "number" ||
            isNaN(lon) ||
            isNaN(lat) ||
            lon < -180 ||
            lon > 180 ||
            lat < -90 ||
            lat > 90
          ) {
            valid = false;
          }
        });
      });
      break;
  }
  return valid;
}

/**
 * @description: 创建点图层
 * @param {String} layerName
 * @param {Array[Array]} points([[lon,lat],[lon,lat],[lon,lat]]) 二维数组
 * @param {Ol.Style} style
 * @return {Ol.VectorLayer}
 * @Author: yx
 */
const createPointsLayer = function (layerName, points, style) {
  const valid = validParams("point", points);
  if (!valid) {
    throw new Error("请检查传入点的格式是否正确");
  }

  const layerNames = this.map
    .getLayers()
    .getArray()
    .map((i) => {
      return i.get("name");
    });
  if (layerNames.includes(layerName)) {
    throw new Error("图层名称重复,请修改layername");
  }

  // 通用样式
  const commonStyle = new this.Style({
    image: new this.Circle({
      radius: 10, // 设置圆的半径，默认为10
      fill: new this.Fill({
        color: "yellow", // 设置填充颜色，默认为黄色
      }),
      stroke: new this.Stroke({
        color: "#000", // 设置描边颜色，默认为黑色
        width: 2, // 设置描边宽度，默认为2
      }),
    }),
  });

  return new this.VectorLayer({
    name: layerName,
    source: new this.VectorSource({
      features: points.map((point) => {
        let feature = new this.Feature({
          geometry: new this.Point(point),
        });
        feature.setStyle(style ? style : commonStyle);
        return feature;
      }),
    }),
  });
};

/**
 * @description: 创建线图层
 * @param {String} layerName
 * @param {*} lines([[[lon,lat],[lon,lat]]]) 三维数组
 * @param {Ol.Style} style
 * @return {Ol.VectorLayer}
 * @Author: yx
 */
const createLinesLayer = function (layerName, lines, style) {
  const valid = validParams("line", lines);
  if (!valid) {
    throw new Error("请检查传入线的格式是否正确");
  }

  const layerNames = this.map
    .getLayers()
    .getArray()
    .map((i) => {
      return i.get("name");
    });
  if (layerNames.includes(layerName)) {
    throw new Error("图层名称重复,请修改layername");
  }

  // 通用样式
  const commonStyle = new this.Style({
    stroke: new this.Stroke({
      color: "#ff6688", // 设置描边颜色，默认为粉红
      width: 3, // 设置描边宽度，默认为2
      lineCap: "round", //线帽样式
      //butt：末端添加平直边缘；round：末端添加圆形线帽；square：末端添加方形线帽；
      lineJoin: "round", //线条连接处样式
      //bevel：创建斜角；round：创建圆角；square：创建尖角；
    }),
  });

  return new this.VectorLayer({
    name: layerName,
    source: new this.VectorSource({
      features: lines.map((line) => {
        let feature = new this.Feature({
          geometry: new this.LineString(line),
        });
        feature.setStyle(style ? style : commonStyle);
        return feature;
      }),
    }),
  });
};

const createPolygonsLayer = function (layerName, polygons, style) {
  // const valid = validParams("polygon", polygons);
  // if (!valid) {
  //   throw new Error("请检查传入面的格式是否正确");
  // }

  const layerNames = this.map
    .getLayers()
    .getArray()
    .map((i) => {
      return i.get("name");
    });
  if (layerNames.includes(layerName)) {
    throw new Error("图层名称重复,请修改layername");
  }

  // 通用样式
  const commonStyle = new this.Style({
    fill: new this.Fill({
      color: "rgba(22, 166, 255, 0.5)", // 设置填充颜色，默认为黄色
    }),
    stroke: new this.Stroke({
      color: "rgb(67, 255, 108)", // 设置描边颜色，默认为粉红
      width: 3, // 设置描边宽度，默认为2
    }),
  });

  return new this.VectorLayer({
    name: layerName,
    source: new this.VectorSource({
      features: polygons.map((polygon) => {
        console.log(polygon);

        let feature = new this.Feature({
          geometry: new this.Polygon(polygon),
        });
        feature.setStyle(style ? style : commonStyle);
        return feature;
      }),
    }),
  });
};

export default {
  createPointsLayer,
  createLinesLayer,
  createPolygonsLayer,
};
